package com.kin207.netty.listener.eventbus;

import java.util.HashSet;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.eventbus.Subscribe;
import com.kin207.netty.listener.Listener;

public class EventBusListener<L extends Listener> {
	final static Logger logger = LoggerFactory.getLogger(EventBusListener.class);
	
	protected Set<L> listeners;
	
	@Subscribe
	public void created(CreatedListenerEvent event) {
		if(listeners!=null){
			for(L item : listeners){
				logger.debug("call Listener created:{}", item);
				item.created(event.getObject());
			}
		}
	}
	
	@Subscribe
	public void destroyed(DestroyedListenerEvent event) {
		if(listeners!=null){
			for(L item : listeners){
				logger.debug("call Listener destroyed:{}", item);
				item.destroyed(event.getObject());
			}
		}
	}
	
	public void addListener(L listener){
		if(listeners==null){
			listeners = new HashSet();
		}
		logger.debug("add listener:{}", listener);
		listeners.add(listener);
	}
	
	public void removeListener(L listener){
		if(listeners!=null){
			listeners.remove(listener);
		}
	}
	
	public Set<L> getListeners() {
		return listeners;
	}
	public void setListeners(Set<L> listeners) {
		this.listeners = listeners;
	}
}
